home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1998 August / PC Plus SuperCD 50b Issue 142 (CD142b) (August 1998).iso / essent / FIXES / CSeries.exe / issue100 / CPROG13.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-31  |  2.4 KB  |  64 lines

  1. /* Print names of .EXE files in current directory of current drive. */
  2.  
  3. #include <stdio.h>
  4. #include <dir.h>
  5. #include <process.h>
  6. #include <dos.h>
  7.  
  8. int main(void)
  9. {
  10. struct ffblk filedetails;    // Create structure in which findfirst() and
  11.                 // findnext() can put details of files they
  12.                 // find. The tag ffblk is defined in DIR.H
  13.  
  14. //                    PATHNAME  ADDRESS OF STRUCTURE OF TYPE FFBLK IN
  15. //                |            |  WHICH TO PUT DETAILS OF FOUND FILE
  16. //                |            |
  17. //                |            |       FILE ATTRIBUTE
  18. //                |            |         |
  19. //              --------  ------------  ---
  20.     if( findfirst("*.EXE",  &filedetails,  0 ) == -1 )
  21.         {
  22.         printf("Nothing found!\n");
  23.         exit(0);  // If no match (findfirst() returned -1) then exit program
  24.         }
  25.  
  26.     do {    // If program gets this far, there must be a name to print
  27.         printf("%s\n", filedetails.ff_name);
  28.     } while( findnext(&filedetails) == 0 ); // Now only loop if there is
  29.                         // another filename. Return
  30.                         // value of 0 indicates
  31.                         // success. See note below.
  32.     exit(-1);    // Flags success as DOS ERRORLEVEL = -1
  33. }
  34.  
  35. /* NOTES
  36. 1: When you want to say 'while value is zero' you can alternatively write
  37. 'while not value':
  38.  
  39.     while ( ! value )
  40.  
  41. ! means 'not'. Remember that a non-zero value equates as true, while zero
  42. equates as false. A loop of 'while(value)' would only continue as long as
  43. value isn't zero. Not (!) flips the result of a test to the opposite of its
  44. truth or falsehood, so 'while( ! value )' is true as long as value is zero.
  45. The loop condition above could therefore be written:
  46.  
  47.     while( ! findnext(&filedetails) )
  48.  
  49. 2: Load DIR.H to see the template that the structure tag ffblk defines.
  50. Alternatively, put the cursor on ffblk and press Ctrl+Shift+F1. In it
  51. you will find a char array called ff_name[] into which the name of the found
  52. file is placed. Filedetails.ff_name is therefore a pointer to this string
  53. which the %s(tring) in printf() picks up.
  54.  
  55. 3: The use of a do...while loop enables us to print the filename even if it
  56. is the only one - the first printf() occurs before the loop condition has been
  57. tested. If an ordinary while loop been used, a duplicate printf() line would
  58. have been needed before the start of the loop. Otherwise, in a case where
  59. there was only one file, the loop test would fail and its name wouldn't be
  60. printed.
  61.  
  62. 4: File attribute in findfirst() - more info in CPROG15.CPP.
  63. */
  64.